<template>
<el-row style="margin-top: 180px;z-index: 1;" type="flex" justify="center">
  <el-col :span="9">
    <el-card element-loading-background="rgba(0, 0, 0, 0.8)">
      <el-form :model="userLoginForm" :rules="userLoginFormRules" ref="userLoginForm">
        <h1>{{$t('app.title')}}</h1>
        <el-form-item prop="username">
          <el-input type="text" clearable v-model="userLoginForm.username" auto-complete="new-password"
                    :placeholder="$t('login.username')"
                    suffix-icon="el-icon-bell" @keyup.enter.native="userLoginFormSubmit"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="userLoginForm.password" auto-complete="new-password"
                    show-password
                    clearable
                    :placeholder="$t('login.password')"
                    suffix-icon="el-icon-edit" @keyup.enter.native="userLoginFormSubmit"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="userLoginFormSubmit()" class="pull-right" style="width:100%">
            {{$t('login.logIn')}}
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </el-col>
</el-row>
</template>

<style lang="scss">
@import './login.scss';
</style>

<script>
export default {
  data () {
    return {
      userLoginForm: {
        username: '',
        password: ''
      },
      userLoginFormRules: {
        username: [
          {required: true, message: `${this.$t('login.username_required_message')}`, trigger: 'blur'},
          {min: 1, max: 12, message: `${this.$t('login.username_min_max_message')}`, trigger: 'blur'}
        ],
        password: [
          {required: true, message: `${this.$t('login.password_required_message')}`, trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    userLoginFormSubmit () {
      this.$refs.userLoginForm.validate((valid) => {
        if (!valid) {
          return false
        }

        this.$http({
          url: '/cardone/v1/usercenter/user/m0001.json',
          method: 'post',
          data: this.userLoginForm
        }).then(response => {
          this.$cookies.set('username', this.userLoginForm.username)
          this.$cookies.set('token', response.data.token)

          window.location.href = '/'
        })
      })
    }
  }
}
</script>
